package vista;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.Utilidades;

import modelo.Cliente;
import modelo.Status;
import modelo.Sucursal;
import modelo.dao.Servidor;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgSeleccionarSucursal extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblCliente;
	private JScrollPane jScrollPane1;
	private JButton btAgregar;
	private JPanel jPanel1;
	private JButton btAceptar;
	private JButton btSalir;
	private JTable tbSucursales;
	private JTextField txtCliente;
	private ArrayList<Sucursal> sucursales;
	private AbstractTableModel tbModelSucursales;
	private Cliente cliente=null;
	private Sucursal sucursal=null;
	
	public DlgSeleccionarSucursal(JDialog dialog,Cliente cli) {
		super(dialog);
		cliente=cli;
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Seleccionar Sucursal");
				{
					lblCliente = new JLabel();
					getContentPane().add(lblCliente);
					lblCliente.setText("Cliente:");
					lblCliente.setBounds(61, 21, 50, 14);
					lblCliente.setFont(new java.awt.Font("Tahoma",1,11));
				}
				{
					txtCliente = new JTextField();
					getContentPane().add(txtCliente);
					txtCliente.setBounds(117, 13, 184, 28);
					txtCliente.setEnabled(false);
					txtCliente.setText("EPA C.A");
				}
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(329, 271, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(228, 271, 79, 40);
				}
				{
					jPanel1 = new JPanel();
					getContentPane().add(jPanel1);
					jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					jPanel1.setBounds(38, 75, 347, 174);
					jPanel1.setLayout(null);
					{
						jScrollPane1 = new JScrollPane();
						jPanel1.add(jScrollPane1);
						jScrollPane1.setBounds(18, 17, 308, 100);
						{
							sucursales = Servidor.clientes().consultarSucursalesPorClienteYStatus(cliente.getCodigo(), Status.ACTIVO);
							
							tbModelSucursales = new AbstractTableModel() {
								@Override
								public Object getValueAt(int fil, int col) {
									switch (col) {
									case 0: return  sucursales.get(fil).getDescripcion();
								
									}
									
									return null;
								}
								
								@Override
								public int getRowCount() {
									return sucursales.size();
								}
								
								@Override
								public String getColumnName(int col) {
									switch (col) {
									case 0: return "Sucursal";
									}
									
									return null;
								}
								@Override
								public int getColumnCount() {
									return 1;
								}
							};
							tbSucursales = new JTable();
							jScrollPane1.setViewportView(tbSucursales);
							tbSucursales.setModel(tbModelSucursales);
							tbSucursales.setBounds(86, 128, 261, 73);
						}
					}
					{
						btAgregar = new JButton();
						jPanel1.add(btAgregar);
						btAgregar.setText("Agregar");
						btAgregar.setIcon(new ImageIcon(getClass().getClassLoader().getResource("iconos/Agregar.png")));
						btAgregar.setBounds(236, 129, 98, 28);
					}
				}
			}
			this.setSize(433, 363);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	  DlgSeleccionarSucursal dssuc=this;
	   public Sucursal mostrar(JDialog dialog) {
			
			setLocationRelativeTo(null);
			txtCliente.setText(cliente.getNombre());
			
			btAgregar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					DlgSucursal dsuc=new DlgSucursal(dssuc);
					dsuc.mostrarIncluir(dssuc,cliente);
					sucursales = Servidor.clientes().consultarSucursalesPorClienteYStatus(cliente.getCodigo(), Status.ACTIVO);
					tbModelSucursales.fireTableDataChanged();
				}
			});
			
			btAceptar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(Utilidades.seleccion(tbSucursales, dssuc, "Sucursal"))
					{ 	sucursal=sucursales.get(dssuc.tbSucursales.getSelectedRow());
						dssuc.dispose();
					}
				}
			});
			
			btSalir.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					dssuc.dispose();
				}
			});
			
			this.setModalityType(ModalityType.APPLICATION_MODAL);
			setVisible(true);
			return sucursal;
			
		}

}
